package org.linlinjava.litemall.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.LitemallUserVo;
import org.linlinjava.litemall.db.domain.LookMoneyLog;
import org.linlinjava.litemall.db.domain.YeJI;
import org.linlinjava.litemall.db.service.LitemallOrderService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 用户服务
 */
@RestController
@RequestMapping("/wx/user")
@Validated
public class WxUserController {
    private final Log logger = LogFactory.getLog(WxUserController.class);

    @Autowired
    private LitemallOrderService orderService;


    @Autowired
    private LitemallUserService litemallUserService;


    /**
     * 用户个人页面数据
     * <p>
     * 目前是用户订单统计信息
     *
     * @param userId 用户ID
     * @return 用户个人页面数据
     */
    @GetMapping("index")
    public Object list(@LoginUser Integer userId) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }

        Map<Object, Object> data = new HashMap<Object, Object>();
        data.put("order", orderService.orderInfo(userId));
        return ResponseUtil.ok(data);
    }


    @GetMapping("/teamList")
    public Object teamList(@LoginUser Integer userId) {

        if (userId == null) {
            return ResponseUtil.unlogin();
        }


        List<LitemallUserVo> userVoList = new ArrayList<LitemallUserVo>();


        List<LitemallUser> userList = litemallUserService.findByUserlian(userId);


        if (userList != null && userList.size() > 0) {

            for (LitemallUser user : userList) {
                YeJI yeJI = litemallUserService.getyejibyuserid(user.getId());

                LitemallUserVo data = new LitemallUserVo();
                data.setNickname(user.getNickname());
                data.setAvatar(user.getAvatar());
                data.setGender(user.getGender());
                data.setMobile(user.getMobile());
                data.setUserLevel(user.getUserLevel());
                data.setSeller(user.getSeller());
                data.setServeCenter(user.getServeCenter());
                data.setTeamtMember(yeJI.getTdrr());
                data.setTotalYJ(yeJI.getTdyj());
                data.setTodayYJ(yeJI.getGryj());

                userVoList.add(data);
            }


            return ResponseUtil.okList(userVoList);

        } else {
            return ResponseUtil.okList(userVoList);
        }

    }


    @GetMapping("/teamLevelCount")
    public Object team(@LoginUser Integer userId) {

        Integer T0 = 0;
        Integer T1 = 0;
        Integer T2 = 0;
        Integer T3 = 0;
        Integer T4 = 0;
        Integer T5 = 0;

        System.out.println(userId);


        List<LitemallUser> userList = litemallUserService.findByUserlian(userId);

        if (userList != null) {
            for (LitemallUser user : userList) {

                if (user.getUserLevel() == 0) {
                    T0 += 1;
                }
                if (user.getUserLevel() == 1) {
                    T1 += 1;
                }
                if (user.getUserLevel() == 2) {
                    T2 += 1;
                }
                if (user.getUserLevel() == 3) {
                    T3 += 1;
                }
                if (user.getUserLevel() == 4) {
                    T4 += 1;
                }
                if (user.getUserLevel() == 5) {
                    T5 += 1;
                }
            }
        }


        Map<Object, Object> data = new HashMap<Object, Object>();
        data.put("T0", T0);
        data.put("T1", T1);
        data.put("T2", T2);
        data.put("T3", T3);
        data.put("T4", T4);
        data.put("T5", T5);

        return ResponseUtil.ok(data);

    }


    @GetMapping("/moneyDetail")
    public Object moneydetail(@LoginUser Integer userId) {
        List<LookMoneyLog> moneyLogList = litemallUserService.selectMyrizhi(userId);

        return ResponseUtil.ok(moneyLogList);
    }


    @GetMapping("/mysq")
    public Object Mysq(@LoginUser Integer userId) {

        Integer T0 = 0;
        Integer T1 = 0;
        Integer T2 = 0;
        Integer T3 = 0;
        Integer T4 = 0;
        Integer T5 = 0;


        LitemallUser users = litemallUserService.findById(userId);
        String  mylian=users.getUserlian();
        String[]   lians=mylian.split(",");

        List<LitemallUser> userList = litemallUserService.findByUserlian(userId);

        if (userList != null) {
            for (LitemallUser user : userList) {

                if (user.getUserLevel() == 0) {
                    T0 += 1;
                }
                if (user.getUserLevel() == 1) {
                    T1 += 1;
                }
                if (user.getUserLevel() == 2) {
                    T2 += 1;
                }
                if (user.getUserLevel() == 3) {
                    T3 += 1;
                }
                if (user.getUserLevel() == 4) {
                    T4 += 1;
                }
                if (user.getUserLevel() == 5) {
                    T5 += 1;
                }
            }
        }

        YeJI yeJI = litemallUserService.getyejibyuserid(userId);

        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        List<LitemallUserVo> userVoList = new ArrayList<LitemallUserVo>();


     List<LitemallUser> userLists = litemallUserService.findByUserlian(userId);
        if (userLists != null && userLists.size() > 0) {

            for (LitemallUser user : userLists) {
                String  helian=user.getUserlian();
                String[]  hearr=helian.split(",");
                if(users.getId().toString().equals(hearr[hearr.length-2])){


                YeJI yeJIs = litemallUserService.getyejibyuserid(user.getId());

                LitemallUserVo data = new LitemallUserVo();
                data.setNickname(user.getNickname());
                data.setAvatar(user.getAvatar());
                data.setGender(user.getGender());
                data.setMobile(user.getMobile());
                data.setUserLevel(user.getUserLevel());
                data.setSeller(user.getSeller());
                data.setServeCenter(user.getServeCenter());
                data.setTeamtMember(yeJIs.getTdrr());
                data.setTotalYJ(yeJIs.getTdyj());
                data.setTodayYJ(yeJIs.getGryj());

                userVoList.add(data);
                }
            }
        }
        Map<Object, Object> data = new HashMap<Object, Object>();
        data.put("T0", T0);
        data.put("T1", T1);
        data.put("T2", T2);
        data.put("T3", T3);
        data.put("T4", T4);
        data.put("T5", T5);
        data.put("YeJi", yeJI);
        data.put("userVoList", userVoList);
        data.put("users", users);
        return ResponseUtil.ok(data);
    }


    @PostMapping("/modify")
    public Object modifyUser(@LoginUser Integer userId, @RequestBody LitemallUserVo userVo) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        int flag = litemallUserService.modifyUser(userId, userVo);

        return ResponseUtil.ok();
    }

}